package com.leetcode;

/**
 * @Classname Leetcode1143
 * @Description TODO
 * @Date 2021-08-25 22:37
 * @Created by darrren
 */
public class Leetcode1143 {
    public int longestCommonSubsequence(String text1, String text2) {
        int m = text1.length(), n = text2.length();
        int[][] dp = new int[m + 1][n + 1];
        for (int i = 1; i <= m; i++) {
            char c1 = text1.charAt(i - 1);
            for (int j = 1; j <= n; j++) {
                char c2 = text2.charAt(j - 1);
                if (c1 == c2) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
        return dp[m][n];
    }

    int power1(int a, int n) {
        if (n == 0) {
            return 1;
        }

        if (n < 0) {
            return 1 / power1(a, -n);
        }

        int res = 1;
        int temp = a;
        while (n != 0) {
            if ((n & 1) == 1) {
                res *= temp;
            }

            n >>= 1;
            temp *= temp;
        }

        return res;
    }

    int power2(int a, int n) {
        if (n == 0) {
            return 1;
        }

        if (n < 0) {
            return power2(a, -n);
        }

        int r = power2(a, n / 2);
        return (n % 2) == 0 ? r * r : r * r * a;
    }

    public static void main(String[] args) {
//        String text1 = "abcde";
//        String text2 = "ace";
//
//        Leetcode1143 leetcode = new Leetcode1143();
//        System.out.println(leetcode.longestCommonSubsequence(text1, text2));
        Leetcode1143 leetcode = new Leetcode1143();
        System.out.println(leetcode.power1(2, 4));
    }
}
